package com.digitalchina.lxnsy.activity.old;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.ViewGroup;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessRealTimePrice;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.TopSortVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.viewholder.ChannelV2LeftHolder;
import com.digitalchina.lxnsy.viewholder.ChannelV2RightHolder;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * ================================================
 * 项目名称：BigData
 * 类描述：包含两级分类
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2017/11/29 13:58
 * 修改备注：
 * ================================================
 */
public class ChannelV2Activity extends BaseActivity {


    @Bind(R.id.recycler_left)
    EasyRecyclerView recyclerLeft;
    @Bind(R.id.recycler_right)
    EasyRecyclerView recyclerRight;

    private RecyclerArrayAdapter adapterLeft;
    private RecyclerArrayAdapter adapterRight;
    private List<TopSortVO> leftList = new ArrayList<>(); // 左侧(1级分类)
    private List<TopSortVO> rightList = new ArrayList<>(); // 右侧(2级分类)


    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_channel_v2);
        setTitle("农产分类");
    }

    @Override
    public void initMember() {

        recyclerLeft.setLayoutManager(new LinearLayoutManager(this));

        recyclerLeft.addItemDecoration(Utils.setDividerDecoration(this, 1, 0, 0));

        recyclerLeft.setAdapterWithProgress(adapterLeft = new RecyclerArrayAdapter(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new ChannelV2LeftHolder(parent);
            }
        });


        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);

        recyclerRight.setLayoutManager(gridLayoutManager);

        recyclerRight.setAdapterWithProgress(adapterRight = new RecyclerArrayAdapter(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new ChannelV2RightHolder(parent);
            }
        });

        gridLayoutManager.setSpanSizeLookup(adapterRight.obtainGridSpanSizeLookUp(3));


        BusinessRealTimePrice.getTopLevelType(this, mHandler);

    }


    @Override
    public void addListener() {
        adapterLeft.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if (leftList.get(position).isSelect())
                    return;
                for (int i = 0; i < leftList.size(); i++) {
                    leftList.get(i).setSelect(i == position);
                }
                adapterLeft.notifyDataSetChanged();
                adapterRight.removeAll();
                BusinessRealTimePrice.getTopLevelType(ChannelV2Activity.this, leftList.get(position).getCode(), mHandler);
            }
        });
        adapterRight.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent();
                intent.putExtra("selectType", (Serializable) adapterRight.getItem(position));
                setResult(999, intent);
                finish();
            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_GET_TOP_LEVEL_ONE_SUCCESS:
                        leftList = FastJsonUtil.getListBean(msg.obj.toString(), "list", TopSortVO.class);
                        if (leftList != null && leftList.size() > 0) {
                            for (int i = 0; i < leftList.size(); i++) {
                                leftList.get(i).setSelect(i == 0);
                            }
                            BusinessRealTimePrice.getTopLevelType(ChannelV2Activity.this, leftList.get(0).getCode(), mHandler);
                        }
                        adapterLeft.addAll(leftList);
                        break;
                    case MSG.MSG_GET_TOP_LEVEL_TYPE_SUCCESS:
                        rightList.clear();
                        rightList = FastJsonUtil.getListBean(msg.obj.toString(), "list", TopSortVO.class);
                        adapterRight.addAll(rightList);
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {

    }

    @Override
    public void rightOnClick() {

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
